package com.elinkway.infinitemovies.ui.fragment;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.elinkway.infinitemovies.adapter.RankHomeGridAdapter;
import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.async.MoviesHttpAsyncTask;
import com.elinkway.infinitemovies.bean.RankHomeData;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.RankHomeDataParser;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.view.NoScrollGridView;
import com.elinkway.infinitemovies.view.PublicLoadLayout;
import com.le123.ysdq.R;
import com.lvideo.http.bean.LVideoDataHull;

public class RankFragment extends BaseFragment implements OnClickListener {
    public static final String TAG = "RankFragment";
    private boolean isCurPage;
    private RankHomeGridAdapter mGridAdapter;
    private NoScrollGridView mGridView;
    private RequestRankHomeData requestTask;
    private RankHomeData homeData;
    private PublicLoadLayout root;

    private ScrollView mRankScrollView;

    // TODO: Rename and change types and number of parameters
    public static RankFragment newInstance(int position) {
        RankFragment fragment = new RankFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    public RankFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setFragmentClassName(TAG);
        root = UIs.createPage(getActivity(), R.layout.fragment_rank);
        root.loading(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        initGridView(root);

        if (homeData == null) {
            requestTask = new RequestRankHomeData(getActivity());
            requestTask.start();
        }

        return root;
    }

    private void initGridView(View v) {
        mGridView = (NoScrollGridView) v.findViewById(R.id.rank_home_grid);

        mRankScrollView = (ScrollView) root.findViewById(R.id.rank_scrollView);

        Rect frame = new Rect();
        getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int actionBarHeight = getSupportActionBar().getHeight();
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        lp.setMargins(0, actionBarHeight, 0, 0);
        mRankScrollView.setLayoutParams(lp);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            MoviesApplication.getInstance().setLastPageClassName(TAG);
            isCurPage = true;
            if (homeData == null) {
                requestTask = new RequestRankHomeData(getActivity());
                requestTask.start();
            }
            // 相当于Fragment的onResume
        } else {
            isCurPage = false;
            // 相当于Fragment的onPause
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private class RequestRankHomeData extends MoviesHttpAsyncTask<RankHomeData> {

        public RequestRankHomeData(Context context) {
            super(context);
        }

        @Override
        public LVideoDataHull<RankHomeData> doInBackground() {
            String[] records = {"5", "5", "5", "5"};
            return MoviesHttpApi.requestRankHomeData(new RankHomeDataParser(),
                    records);
        }

        @Override
        public void onPostExecute(int updateId, RankHomeData result) {
            homeData = result;
            // TODO Auto-generated method stub
            mGridAdapter = new RankHomeGridAdapter(getActivity(), result);
            mGridView.setAdapter(mGridAdapter);
            root.finish();
        }

        @Override
        public void netNull() {
            // TODO Auto-generated method stub
            super.netNull();
            root.error(false, false);
            root.setmRefreshData(new PublicLoadLayout.RefreshData() {

                @Override
                public void refreshData() {
                    if (requestTask != null) {
                        if (!requestTask.isCancelled()) {
                            requestTask.cancel();
                            requestTask = null;
                        }
                    }
                    requestTask = new RequestRankHomeData(getActivity());
                    requestTask.start();
                }
            });

        }

        @Override
        public void netErr(int updateId, String errMsg) {
            // TODO Auto-generated method stub
            super.netErr(updateId, errMsg);
            root.error(false, false);
            root.setmRefreshData(new PublicLoadLayout.RefreshData() {

                @Override
                public void refreshData() {
                    if (requestTask != null) {
                        if (!requestTask.isCancelled()) {
                            requestTask.cancel();
                            requestTask = null;
                        }
                    }
                    requestTask = new RequestRankHomeData(getActivity());
                    requestTask.start();
                }
            });
        }

        @Override
        public void dataNull(int updateId, String errMsg) {
            root.error(false, false);
            root.setmRefreshData(new PublicLoadLayout.RefreshData() {

                @Override
                public void refreshData() {
                    if (requestTask != null) {
                        if (!requestTask.isCancelled()) {
                            requestTask.cancel();
                            requestTask = null;
                        }
                    }
                    requestTask = new RequestRankHomeData(getActivity());
                    requestTask.start();
                    root.loading(false);
                }
            });
            super.dataNull(updateId, errMsg);
        }
    }
}
